package com.aoji.utils.ueditor;

import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.baidu.ueditor.hunter.FileManager;
import com.baidu.ueditor.hunter.ImageHunter;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
public class ActionEnter {
        private HttpServletRequest request = null;
        private String rootPath = null;
        private String contextPath = null;
        private String actionType = null;
        private ConfigManager configManager = null;

        public ActionEnter(HttpServletRequest request, String rootPath) {
            this.request = request;
            this.rootPath = rootPath;
            this.actionType = request.getParameter("action");
            this.contextPath = request.getContextPath();
            this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, request.getRequestURI());
        }

        public String exec()  {
            String callbackName = this.request.getParameter("callback");
            if (callbackName != null) {
                return !this.validCallbackName(callbackName) ? (new BaseState(false, 401)).toJSONString() : callbackName + "(" + this.invoke() + ");";
            } else {
                return this.invoke();
            }
        }

        public String invoke()  {
            if (this.actionType != null && ActionMap.mapping.containsKey(this.actionType)) {
                if (this.configManager != null ) {
                    State state = null;
                    int actionCode = ActionMap.getType(this.actionType);
                    Map<String, Object> conf = null;
                    switch(actionCode) {
                        case 0:
                            return this.configManager.getAllConfig()+"";
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                            System.out.println("获取配置信息……");
                            conf = this.configManager.getConfig(actionCode);
                            System.out.println();
                            state = (new Uploader(this.request, conf)).doExec();
                            break;
                        case 5:
                            conf = this.configManager.getConfig(actionCode);
                            String[] list = this.request.getParameterValues((String)conf.get("fieldName"));
                            state = (new ImageHunter(conf)).capture(list);
                            break;
                        case 6:
                        case 7:
                            conf = this.configManager.getConfig(actionCode);
                            int start = this.getStartIndex();
                            state = (new FileManager(conf)).listFile(start);
                    }
                    System.out.println("state:"+state.toJSONString());
                    return state.toJSONString();
                } else {
                    return (new BaseState(false, 102)).toJSONString();
                }
            } else {
                return (new BaseState(false, 101)).toJSONString();
            }
        }

        public int getStartIndex() {
            String start = this.request.getParameter("start");

            try {
                return Integer.parseInt(start);
            } catch (Exception var3) {
                return 0;
            }
        }

        public boolean validCallbackName(String name) {
            return name.matches("^[a-zA-Z_]+[\\w0-9_]*$");
        }

}
